package com.xw.service.sysconfig;

import com.xw.dao.DicGroupMapper;
import com.xw.model.DicGroup;
import com.xw.model.User;
import com.xw.service.common.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class DicGroupServiceImpl extends BaseServiceImpl<DicGroup> implements DicGroupService {
    Map<String,Object> map=null;
    @Resource
    private DicGroupMapper groupMapper;
    @Resource
    private UserService userService;
    @Resource
    private DicGroupLimitService groupLimitService;

    @Override
    public List<DicGroup> findByWhere(DicGroup model) {
        return groupMapper.findByWhere(model);
    }

    /**
     * 删除分组前要判断 1.是否有用户拥有这个权限 2.删除角色菜单关联表
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> del(String id) {
        map = new HashMap<>();
        //删除分组前要判断 1.是否有用户拥有这个权限 2.删除角色菜单关联表
        User user = new User();
        user.setGroupId(id);
        Integer num = userService.queryCount(user);
        if (num>0){
            map.put("code",101);
            map.put("msg","此分组已被使用，不能删除");
            return map;
        }
        //删除分组权限关联表
        Integer count=groupLimitService.delByGroupId(id);
        if (count>0){
            this.deleteByUUId(id);
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",101);
            map.put("msg","删除失败");
        }
        return map;
    }
}
